﻿Public Class ApplicationMain

	Public autoCompleteCollection As New AutoCompleteStringCollection

	Public Sub AddAutoComplete(item As String)
		If item <> "" Then
			If My.Settings.auto_complete Then
				If Not autoCompleteCollection.Contains(item) Then
					If autoCompleteCollection.Count < 50 Then
						autoCompleteCollection.Add(item)
						My.Settings.auto_complete_collection.Add(item)

					Else
						autoCompleteCollection.RemoveAt(0)
						autoCompleteCollection.Add(item)
						My.Settings.auto_complete_collection.RemoveAt(0)
						My.Settings.auto_complete_collection.Add(item)

					End If
				End If
			End If
		End If
	End Sub

	Public Sub getCharts(limit As Integer, page As Integer)
		Dim methodGetHypedArtists = New ChartGetHypedArtists(limit, page)
		methodGetHypedArtists.sendRequest()
		Dim responseHypedArtists = methodGetHypedArtists.getResponse()
		ModuleControls.GenerateListViewSearch(ListViewHypedArtists)
		ModuleArtist.AddToListView(responseHypedArtists.artists, ListViewHypedArtists)


		Dim methodGetHypedTracks = New ChartGetHypedTracks(limit, page)
		methodGetHypedTracks.sendRequest()
		Dim responseHypedTracks = methodGetHypedTracks.getResponse()
		ModuleControls.GenerateListViewSearch(ListViewHypedTracks)
		ModuleTrack.AddToListView(responseHypedTracks.tracks, ListViewHypedTracks)


		Dim methodGetTopArtists = New ChartGetTopArtists(limit, page)
		methodGetTopArtists.sendRequest()
		Dim responseTopArtists = methodGetTopArtists.getResponse()
		ModuleControls.GenerateListViewSearch(ListViewTopArtists)
		ModuleArtist.AddToListView(responseTopArtists.artists, ListViewTopArtists)


		Dim methodGetTopTracks = New ChartGetTopTracks(limit, page)
		methodGetTopTracks.sendRequest()
		Dim responseTopTracks = methodGetTopTracks.getResponse()
		ModuleControls.GenerateListViewSearch(ListViewTopTracks)
		ModuleTrack.AddToListView(responseTopTracks.tracks, ListViewTopTracks)


	End Sub


	Private Sub SignOnToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SignOnToolStripMenuItem.Click
		Dim result = FormSignOn.ShowDialog()
		If My.Settings.user_name = "" Or My.Settings.sk = "" Then
			Dim errormsg As MsgBoxResult = MsgBox("To use this program please sign on.", MsgBoxStyle.OkOnly, "Sign On Failed")
		End If
	End Sub

	Private Sub ApplicationMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

		My.Settings.Save()

	End Sub

	Private Sub ApplicationMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		If My.Settings.auto_complete Then
			TextBoxSearch.AutoCompleteMode = AutoCompleteMode.Suggest
		Else
			TextBoxSearch.AutoCompleteMode = AutoCompleteMode.None
		End If

		For Each item In My.Settings.auto_complete_collection
			autoCompleteCollection.Add(item)
		Next

		TextBoxSearch.AutoCompleteCustomSource = autoCompleteCollection

		If My.Settings.user_name = "" Or My.Settings.sk = "" Then
			If Not SplashScreen.IsDisposed Then
				'SplashScreen.Close()
			End If

			Dim result = FormSignOn.ShowDialog()
			If result = Windows.Forms.DialogResult.Cancel Then

				Dim errormsg As MsgBoxResult = MsgBox("You must sign on to use this program.", MsgBoxStyle.OkOnly, "Error: Sign On")
			End If
		End If


		getCharts(10, 1)

	End Sub

	Private Sub ButtonSearchAlbum_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSearchAlbum.Click
		FormSearch.prevForm = Me

		If TextBoxSearch.Text <> "" Then
			FormSearch.SearchAll(TextBoxSearch.Text)
		End If

		FormSearch.Show()

		AddAutoComplete(TextBoxSearch.Text)

		Me.Hide()
	End Sub

	Private Sub AdvancedCommandsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AdvancedCommandsToolStripMenuItem.Click
		Main.Show()
	End Sub

	Private Sub OptionsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OptionsToolStripMenuItem.Click
		Dim result = FormOptions.ShowDialog()

		If result = Windows.Forms.DialogResult.OK Then
			For Each item As ListViewItem In FormOptions.ListViewOptions.Items
				Dim value = item.Checked

				Select Case item.Text
					Case "Auto Complete"
						My.Settings.auto_complete = value
						If value Then
							TextBoxSearch.AutoCompleteMode = AutoCompleteMode.Suggest
						Else
							TextBoxSearch.AutoCompleteMode = AutoCompleteMode.None
						End If
				End Select
			Next
		End If
	End Sub

	Private Sub ListViewHypedArtists_ItemActivate(sender As Object, e As System.EventArgs) Handles ListViewHypedArtists.ItemActivate
		Dim test = FormArtistDisplay.LoadArtist(ListViewHypedArtists.SelectedItems.Item(0).SubItems("Artist Name").Text, ListViewHypedArtists.SelectedItems.Item(0).SubItems("MBID").Text)
		If test = True Then
			FormArtistDisplay.prevForm = Me
			FormArtistDisplay.Show()
			Me.Hide()
		End If
	End Sub

	Private Sub ListViewTopArtists_ItemActivate(sender As Object, e As System.EventArgs) Handles ListViewTopArtists.ItemActivate
		Dim test = FormArtistDisplay.LoadArtist(ListViewTopArtists.SelectedItems.Item(0).SubItems("Artist Name").Text, ListViewTopArtists.SelectedItems.Item(0).SubItems("MBID").Text)
		If test = True Then
			FormArtistDisplay.prevForm = Me
			FormArtistDisplay.Show()
			Me.Hide()
		End If
	End Sub

	Private Sub ButtonTasteometer_Click(sender As System.Object, e As System.EventArgs) Handles ButtonTasteometer.Click
		FormTasteometer.prevForm = Me
		FormTasteometer.Show()
		Me.Hide()
	End Sub

	Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
		AboutBox.ShowDialog()
	End Sub

	Private Sub NumericUpDown1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles NumericUpDown1.ValueChanged
		getCharts(10, NumericUpDown1.Value)
	End Sub
End Class